package com.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;

import java.util.Random;
import java.util.concurrent.Future;

class AsyncTask {

    private static Random random = new Random();

    @Async
    static Future<String> doTaskOne() throws InterruptedException {

        System.out.println("开始你的任务一");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));

        long end = System.currentTimeMillis();

        System.out.println("完成任务一：耗时："+ (end - start)+ "毫秒");
        return new AsyncResult<>("完成了任务一");

    }

    @Async
    static Future<String> doTaskTwo() throws InterruptedException {

        System.out.println("开始你的任务二");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));

        long end = System.currentTimeMillis();

        System.out.println("完成任务一：耗时："+ (end - start)+ "毫秒");
        return new AsyncResult<>("完成了任务二");

    }

    @Async
    static Future<String> doTaskThree() throws InterruptedException {

        System.out.println("开始你的任务三");
        long start = System.currentTimeMillis();
        Thread.sleep(random.nextInt(10000));

        long end = System.currentTimeMillis();

        System.out.println("完成任务一：耗时："+ (end - start)+ "毫秒");
        return new AsyncResult<>("完成了任务三");

    }


}
